package com.yanqu.road.server.cmd.kuang;

import com.yanqu.road.entity.kuang.KuangConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.kuang.Kuang;
import com.yanqu.road.server.cmd.CrossAppointCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.kuang.CrossKuangMgr;
import com.yanqu.road.server.manager.kuang.activity.CrossKuangActivity;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_CROSS_KUANG_CHOOSE_TREASURE, desc = "选择阵营宝箱")
public class CrossKuangChooseTreasureCmd extends CrossAppointCommand {

    private int groupId;

    private int position;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        Kuang.CrossKuangChooseTreasureReqMsg reqMsg = Kuang.CrossKuangChooseTreasureReqMsg.parseFrom(packet.getBytes());
        groupId = reqMsg.getGroupId();
        position = reqMsg.getPosition();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossKuangActivity activity = CrossKuangMgr.getActivity(groupId);
        if (activity == null) {
            ret = GameErrorCode.E_KUANG_BATTLE_ACTIVITY_NOT_FOUND;
            return;
        }
        KuangConfig config = activity.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_KUANG_CONFIG_NOT_FOUND;
            return;
        }
        ret = activity.chooseTreasureByCmd(userId, position);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            Kuang.CrossKuangChooseTreasureRespMsg.Builder respMsg = Kuang.CrossKuangChooseTreasureRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.S_KUANG_CHOOSE_TREASURE_FROM_CROSS, respMsg));
        }
    }
}
